package com.ruoyi.project.hcy.task.controller;

import com.ruoyi.common.constant.UserConstants;
import com.ruoyi.framework.aspectj.lang.annotation.Log;
import com.ruoyi.framework.aspectj.lang.enums.BusinessType;
import com.ruoyi.framework.web.controller.BaseController;
import com.ruoyi.framework.web.domain.AjaxResult;
import com.ruoyi.framework.web.page.TableDataInfo;
import com.ruoyi.project.hcy.administrations.domain.Branch;
import com.ruoyi.project.hcy.administrations.domain.Crew;
import com.ruoyi.project.hcy.task.domain.Task;
import com.ruoyi.project.hcy.task.service.ITaskService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;


/**
 * 任务操作处理
 */
@Controller
@RequestMapping("/hcy/task/taskmanager")
public class TaskController extends BaseController {

    @Autowired
    ITaskService taskService;

    private String prefix = "hcy/task/taskmanager";

    @RequiresPermissions("hcy:task:view")
    @GetMapping()
    public String task() {

        return prefix + "/task";

    }

    @RequiresPermissions("hcy:task:result")
    @GetMapping("/result")
    public String result() {

        return prefix + "/result";

    }

    @RequiresPermissions("hcy:task:view")
    @GetMapping("/taskaccept")
    public String taskaccept() {

        return prefix + "/taskaccept";

    }

    @RequiresPermissions("hcy:task:view")
    @GetMapping("/taskissue")
    public String taskissue() {

        return prefix + "/taskissue";

    }

    @RequiresPermissions("hcy:task:add")
    @GetMapping("/add")
    public String add() {

        return prefix + "/add";

    }

    @RequiresPermissions("hcy:task:remove")
    @Log(title = "任务管理", businessType = BusinessType.DELETE)
    @PostMapping("/remove")
    @ResponseBody
    public AjaxResult remove(String ids) {
        try {
            return toAjax(taskService.deleteTaskByIds(ids));
        } catch (Exception e) {
            return error(e.getMessage());
        }
    }

    /**
     * 新增保存任务
     */
    @RequiresPermissions("hcy:task:add")
    @Log(title = "任务管理", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(@Validated Task task, Long executorId, String taskStartTime, String taskEndTime) {
        if (UserConstants.POST_NAME_NOT_UNIQUE.equals(taskService.checkTaskTitleUnique(task))) {
            return error("新增任务'" + task.getTaskTitle() + "'失败，任务标题已存在");
        }
        return toAjax(taskService.insertTask(task, executorId, taskStartTime, taskEndTime));
    }

    @RequiresPermissions("hcy:task:add")
    @PostMapping("/crewList")
    @ResponseBody
    public List<Crew> crewList(String deptName, ModelMap mmap) {
        List<Crew> crews = taskService.selectCrews(deptName);
        mmap.put("crews", crews);
        return crews;
    }

    @RequiresPermissions("hcy:task:add")
    @PostMapping("/deptList")
    @ResponseBody
    public List<Branch> deptList() {
        return taskService.selectBranchs();
    }

    @RequiresPermissions("hcy:task:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(Task task) {
        startPage();
        List<Task> list = taskService.selectTaskList(task);
        return getDataTable(list);
    }

    @RequiresPermissions("hcy:task:list")
    @PostMapping("/list2")
    @ResponseBody
    public TableDataInfo list2(Task task) {
        startPage();
        List<Task> list = taskService.selectTaskList2(task);
        return getDataTable(list);
    }


    @GetMapping("/submit/{postId}")
    public String submit(@PathVariable("postId") Long taskId, ModelMap mmap) {
        mmap.put("task", taskService.selectTaskById(taskId));
        return prefix + "/resultsubmit";
    }

    @RequiresPermissions("hcy:task:edit")
    @Log(title = "岗位管理", businessType = BusinessType.UPDATE)
    @PostMapping("/submit")
    @ResponseBody
    public AjaxResult submitSave(@Validated Task task) {
        return toAjax(taskService.taskSubmit(task));
    }

    /**
     * 修改任务
     */
    @GetMapping("/edit/{postId}")
    public String edit(@PathVariable("postId") Long taskId, ModelMap mmap) {
        Task task = taskService.selectTaskById(taskId);
        String taskFile = task.getTaskFile();
        String newTaskFile = "/img/hcy/task/" + taskFile;
        System.out.println(newTaskFile);
        task.setTaskFile(newTaskFile);
        mmap.put("task", task);
        mmap.put("depts", taskService.selectBranchs());
        return prefix + "/edit";
    }

    /**
     * 修改保存任务
     */
    @RequiresPermissions("hcy:task:edit")
    @Log(title = "岗位管理", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(@Validated Task task, Long executorId) {
        if (UserConstants.POST_NAME_NOT_UNIQUE.equals(taskService.checkTaskTitleUnique(task))) {
            return error("修改任务'" + task.getTaskTitle() + "'失败，任务名称已存在");
        }
        return toAjax(taskService.updateTask(task, executorId));
    }

    /**
     * 校验任务名称
     */
    @PostMapping("/checkTaskTitleUnique")
    @ResponseBody
    public String checkTaskTitleUnique(Task task) {
        return taskService.checkTaskTitleUnique(task);
    }

}
